package com.xzb.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xzb.demo.utils.CertUtil;
import com.xzb.demo.utils.CryptoUtil;
import com.xzb.demo.utils.HttpUtil;
import com.xzb.demo.utils.RandomStringGenerator;
import com.xzb.demo.vo.RequestVO;
import com.xzb.demo.vo.payment.req.PaymentReqListVO;
import com.xzb.demo.vo.payment.req.PaymentReqVO;
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/**
 * @Author：kadaa
 * @Date：2023/4/12 15:57
 * @Description:
 */

@RestController
@RequestMapping("band/card")
public class CashController {


    public static String gatewayUrl = "https://taxpay.linkscloud.net/api/";
    //平台公钥
    public static String public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmoGdZ507lfxCBeI05tZfhl3yAA+ecYr2sEJrYmCqTNKj0aqlzEGvX7J36u9kxBXq5w1BSXxHrXKQY8QdGBqIXO7dydU36vO2CXSfp/uVHf0rrwa5CleSB/IYIJw7zsN5ecPbgljykcCYZYaakQLoEdaQAy+Qkag0qRdMnTLExHUlLTZjotZeETyHy7EKAQxKUJ4iWKBfSsVgW/Clz/TXm1wkId8b81XLCXz/vWJasFx6yOvv6aSEIINecHzo7Soz+rd2bCFFXcZdf3vH8mQ7PvswFcBw1hq/qiLv2ZwSnPxr4LIhVYeWSAIcNwxDcMm4YAPNwC5ma4a+OLu+aELKlQIDAQAB";
    //商户私钥
    public static String private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCagZ1nnTuV/EIF4jTm1l+GXfIAD55xivawQmtiYKpM0qPRqqXMQa9fsnfq72TEFernDUFJfEetcpBjxB0YGohc7t3J1Tfq87YJdJ+n+5Ud/SuvBrkKV5IH8hggnDvOw3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Sz9ehhCqc9zarbEgB6/2iWp0JJgvjTEAhwKBgFQPzi28J6KJfVjV28VtcfOSt2lK84Md+RKeQqVzj6QIhdhvodv8eCCve8NoNMD+KyzgERhefRv44VbwNIiDYzS7fq1I6R9kcNqhWVak3TkXFuPiagK+7/s/CDFG2ZopTqyin1T4e7UsJ2YJR/9I9fTwbbJEraW0m3tqniZPO5FLAoGAShwNS5IUEd4NhtiWoinMYILHYe0Qro8lEZLE5I9seZwDf0d0UpzO6s8YsxgsIpHm0+XjLVjxn2jAWg6l1aGy5nKv1mPEVK0HOZpwAv0fd293fCqQA2gUqlgm71zsMIHTzd6l8d0zV3DJf3xMqzTedROCeCd8ciVzEYDsOFlQz1c=";

    public static final String merchantNo = "MI384942196221935616";



    @PostMapping("/transfer/to/bank")
    public String testTransferToBank() throws Exception {
        CertUtil.initPrivateKey(private_key);

        CertUtil.initPulbicKey(public_key);


        PaymentReqVO paymentVO = new PaymentReqVO(String.valueOf(System.currentTimeMillis()), new BigDecimal("1"), "潘海文", "6222032010019265110", "440903200006061514",
                "13682273695", "102602002118", "中国工商银行", "广东省", "广州市", "测试付款", "0", "2", "1");
        PaymentReqListVO paymentReqListVO = new PaymentReqListVO();
        paymentReqListVO.setBatchNo(String.valueOf(System.currentTimeMillis()));
        paymentReqListVO.setMerchantNo(merchantNo);
        paymentReqListVO.setTotalAmt(paymentVO.getPayAmount());

        ArrayList<PaymentReqVO> paymentVOArrayList = new ArrayList<>();
        paymentVOArrayList.add(paymentVO);

        paymentReqListVO.setPayItems(paymentVOArrayList);
        paymentReqListVO.setTotalCount(paymentVOArrayList.size());


        String reqData = JSONObject.toJSONString(paymentReqListVO);


        String aesKey = RandomStringGenerator.getRandomStringByLength(16);

        byte[] aesKeyBytes = aesKey.getBytes("UTF-8");

        byte[] plainBytes = reqData.getBytes("UTF-8");
        String encryptData = new String(Base64.encodeBase64(
                CryptoUtil.AESEncrypt(plainBytes, aesKeyBytes, "AES",
                        "AES/ECB/PKCS5Padding", null)),
                "UTF-8");

        String sign = new String(Base64.encodeBase64(
                CryptoUtil.digitalSign(plainBytes, CertUtil.getPrivateKey(),
                        "SHA1WithRSA")), "UTF-8");

        String encryptKey = new String(Base64.encodeBase64(
                CryptoUtil.RSAEncrypt(aesKeyBytes, CertUtil.getPublicKey(), 2048, 11,
                        "RSA/ECB/PKCS1Padding")), "UTF-8");


        RequestVO requestVO = new RequestVO();
        requestVO.setMerchantNo(merchantNo);
        requestVO.setEncryptData(encryptData);
        requestVO.setEncryptKey(encryptKey);
        requestVO.setSign(sign);


        System.out.println("请求参数为：" + JSONObject.toJSONString(requestVO));


        String res = HttpUtil.doPost(gatewayUrl + "/settle/v1/pay", JSONObject.parseObject(JSONObject.toJSONString(requestVO)), "UTF-8");

        System.out.println("出款返回为" + res);

        JSONObject resJson = JSON.parseObject(res);
        RequestVO resVO = JSON.parseObject(resJson.getString("data"), RequestVO.class);
        System.out.println(resVO.toString());




        //解密验证
        String resSign = resVO.getSign();
        String resEncryptData = resVO.getEncryptData();
        String resEncryptKey = resVO.getEncryptKey();
        String resMerchantNo = resVO.getMerchantNo();




        byte[] decodeBase64KeyBytes = Base64.decodeBase64(resEncryptKey.getBytes(StandardCharsets.UTF_8));
        byte[] aESKeyBytes = CryptoUtil.RSADecrypt(
                decodeBase64KeyBytes, CertUtil.getPrivateKey(), 2048, 11,
                "RSA/ECB/PKCS1Padding");

        byte[] decodeBase64DataBytes = Base64.decodeBase64(resEncryptData.getBytes(StandardCharsets.UTF_8));

        byte[] respDataBytes = CryptoUtil.AESDecrypt(decodeBase64DataBytes,
                aESKeyBytes, "AES", "AES/ECB/PKCS5Padding", null);


        byte[] signBytes = Base64.decodeBase64(resSign.getBytes(StandardCharsets.UTF_8));

        boolean isValid = CryptoUtil.verifyDigitalSign(respDataBytes, signBytes,
                CertUtil.getPublicKey(), "SHA1WithRSA");

        String resData = new String(respDataBytes, StandardCharsets.UTF_8);
        System.out.println(resData);

        System.out.println(isValid);
        return "success";

    }

}
